<template>
  <div class="queue-container app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <div class="filter-form">
        <el-form :model="filter" :inline="true" size="medium">
          <el-form-item label="">
            <el-input v-model="filter.name" placeholder="输入队列名称" />
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-model="filter.type"
              placeholder="请选类型"
            >
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="">
            <el-select
              v-model="filter.target"
              placeholder="请选对象"
            >
              <el-option
                v-for="item in targets"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="">
            <el-select
              v-model="filter.status"
              placeholder="请选择状态"
            >
              <el-option
                v-for="item in statuses"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div>
          <el-button @click="handleFilter">
            搜索
          </el-button>
          <el-button
            @click="resetFilter"
          >重置
          </el-button>
        </div>
      </div>
    </div>

    <!--列表-->
    <div v-loading="listLoading" class="content-container">
      <el-table
        ref="table"
        :cell-style="{ textAlign: 'center' }"
        :data="list"
        border
        style="width: 100%"
        row-key="id"
        size="mini"
        :header-cell-class-name="headerStyle"
      >
        <el-table-column prop="id" label="队列号" />
        <el-table-column prop="type" label="队列类型">
          <template slot-scope="{ row }">
            <el-tag v-if="row.type === 'import'" type="info">导入</el-tag>
            <el-tag v-else-if="row.type === 'export'" type="info">导出</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="type" label="对象">
          <template slot-scope="{ row }">
            <el-tag v-if="row.target === 'goods'" type="info">商品</el-tag>
            <el-tag v-else-if="row.target === 'coupon'" type="info">卡券</el-tag>
            <el-tag v-else-if="row.target === 'purchase'" type="info">采购</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="名称">
          <template slot-scope="{ row }">
            {{ row.name }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="单据状态">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 'pending'" type="warning">处理中</el-tag>
            <el-tag v-else-if="row.status === 'success'" type="success">处理已完成</el-tag>
            <el-tag v-else-if="row.status === 'fail'" type="info">处理失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否有错误">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 'success' && row.errors.length > 0" type="warning">是</el-tag>
            <el-tag v-else type="success">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="250px" fixed="right">
          <template slot-scope="{ row, $index }">
            <el-button
              v-if="row.status === 'success' && row.errors.length > 0"
              type="primary"
              size="mini"
              plain
              @click="viewErrors(row, $index)"
            >
              查看错误数据
            </el-button>
            <el-button
              v-if="row.status === 'success' && row.errors.length === 0 && row.type === 'export'"
              type="primary"
              size="mini"
              plain
              @click="handleDownload(row, $index)"
            >
              导出文件下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="filter.page"
      :limit.sync="filter.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getQueues,
  downloadQueueErrors
} from '@/api/queue'
import checkPermission from '@/utils/permission' // 权限判断函数
import Pagination from '@/components/Pagination'
import { downloadFileUrl } from '@/utils/index'

export default {
  name: 'QueueList',
  components: { Pagination },
  data() {
    return {
      listLoading: false,
      total: 0,
      types: [
        { label: '导出', value: 'export' },
        { label: '导入', value: 'import' }
      ],
      targets: [
        { label: '商品', value: 'goods' },
        { label: '卡券', value: 'coupon' },
        { label: '采购', value: 'purchase' }
      ],
      statuses: [
        { label: '处理中', value: 'pending' },
        { label: '失败', value: 'fail' },
        { label: '成功', value: 'success' }
      ],
      filter: {
        page: 1,
        limit: 10,
        type: undefined,
        target: undefined,
        status: undefined
      },
      list: [],
      posting: false
    }
  },
  computed: {
  },
  mounted() {
    this.getList()
  },
  methods: {
    checkPermission,
    headerStyle({ row, rowIndex }) {
      return 'table-th'
    },
    /**
     * 查询过滤
     */
    handleFilter() {
      this.filter.page = 1
      this.getList()
    },
    /**
     * 重置过滤
     */
    resetFilter() {
      this.filter.page = 1
      this.filter = Object.assign({}, this.filter, {
        page: 1,
        type: undefined,
        target: undefined,
        status: undefined
      })
      this.getList()
    },
    getList() {
      this.listLoading = true
      getQueues(this.filter).then((response) => {
        this.listLoading = false
        const { list, total } = response.data
        this.list = list
        this.total = total
      })
    },
    refreshList() {
      getQueues(this.filter).then((response) => {
        const { list, total } = response.data
        this.list = list
        this.total = total
      })
    },
    search() {
      this.filter.page = 1
      this.getList()
    },
    resetForm() {
      this.filter.page = 1
      this.filter.type = undefined
      this.filter.target = undefined
      this.filter.status = undefined
      this.getList()
    },
    viewErrors(row, index) {
      downloadQueueErrors({ id: row.id })
    },
    handleDownload(row, index) {
      if (row.url) {
        downloadFileUrl(row.name, row.url)
      }
    }
  }
}
</script>
